package com.iflytek.exam.utils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.core.page.TableDataInfo;

import java.util.List;

public class PageUtil {
    /**
     * 获取页面信息
     *
     * @param list        列表
     * @param currentPage 当前页面
     * @param pageSize    页面大小
     * @return {@link TableDataInfo}
     */
    public static <T> TableDataInfo getPageInfo(List<T> list, Integer currentPage, Integer pageSize) {
        int total = list.size();
        if (total > pageSize) {
            int toIndex = pageSize * currentPage;
            if (toIndex > total) {
                toIndex = total;
            }
            list = list.subList(pageSize * (currentPage - 1), toIndex);
        }
        Page<T> page = new Page<>(currentPage, pageSize);
        page.addAll(list);
        page.setPages((total + pageSize - 1) / pageSize);
        page.setTotal(total);

        PageInfo<T> pageInfo = new PageInfo<>(page);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setRows(list);
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }

}
